#!/usr/bin/env python
# -*- coding: utf-8 -*-

import threading

from controls import mc
from utils import log
from controls import data_sight_desc_title_mysql


def set_sight_desc_title_item_to_mc(pfx, key, item):
    return mc.set_item(pfx, key, item)


def get_sight_desc_title_all():
    try:
        sight_desc_title_item = mc.get_item('sight', 0)
        if sight_desc_title_item is None:
            item = data_sight_desc_title_mysql.get_sight_desc_title_all()
            t = threading.Thread(target=set_sight_desc_title_item_to_mc, args=('sight', 0, item))
            t.start()
            return item
        else:
            return sight_desc_title_item
    except Exception, e:
        log.exp(e)
        return False


def add_sight_desc_title_item(item):
    res_mc = mc.set_item('sight', 0, item)
    if not res_mc:
        return False
    for one in range(0, len(item)):
        res_db = data_sight_desc_title_mysql.add_sight_desc_title_item(item[one])
        if not res_db:
            mc.set_item('sight', 0, None)
            return False


if __name__ == '__main__':
    print get_sight_desc_title_all()
